package gui.util;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class GUIUtil extends JPanel{
       private static  String imageFolder = "D:/JAVA_CODE/hutubills/img";
    //给按钮设置图标和文本以及提示文字
       public static void setImageIcon(JButton b, String fileName,String tip){
           ImageIcon i= new ImageIcon(new File(imageFolder,fileName).getAbsolutePath());
           b.setIcon(i);
           b.setPreferredSize(new Dimension(61,81));
           b.setToolTipText(tip);
           //设置标签的文本相对其图像的水平位置
           b.setVerticalTextPosition(JButton.BOTTOM);
           //设置标签的文本相对其图像的垂直位置
           b.setHorizontalTextPosition(JButton.CENTER);
           b.setText(tip);
       }
   //给多个组件设置前景色
       public  static void setColor(Color color,JComponent... cs){
           for(JComponent c:cs){
               c.setForeground(color);
           }
       }
       public static void showPanel(JPanel p,double serechRate){
           GUIUtil.useLNF();
           JFrame f = new JFrame();
           f.setSize(500,500);
           f.setLocationRelativeTo(null);
           CenterPanel cp = new CenterPanel(serechRate);
           f.setContentPane(cp);
           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           f.setVisible(true);
           cp.show(p);
                  }
    public static void showPanel(JPanel p) {
        showPanel(p,0.85);
    }
    //判断输入框内容是否为正式
        public static boolean checkNumber(JTextField tf,String input){
           if (!checkEmpty(tf,input)){
               return false;
               }
            String text = tf.getText().trim();
            try {
                Integer.parseInt(text);
                return true;
            } catch (NumberFormatException e1) {
                JOptionPane.showMessageDialog(null, input + " 需要是整数");
                tf.grabFocus();
                return false;
            }

        }
        //判断输入框内容是否为零
        public static boolean checkZero(JTextField tf,String input){
           if(!checkEmpty(tf,input))
               return false;
           String text = tf.getText().trim();
            if(Integer.parseInt(text) == 0){
                JOptionPane.showMessageDialog(null,input+"不能为0");
                tf.grabFocus();
                return false;
            }
            return true;
        }
        //判断输入框内容是否为空
        public static boolean checkEmpty(JTextField tf,String input){
           String text = tf.getText().trim();
           if(text.length()==0){
               JOptionPane.showMessageDialog(null,input+"不能为空");
               tf.grabFocus();;
               return false;
           }
           return true;
        }
        public static int getInt(JTextField tf){
           return Integer.parseInt(tf.getText().trim());
        }
        public static void useLNF(){
            try {
                javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}
